/*
 * JTalkerClientFrontPopUp.java
 *
 * Created on 03. April 2005, 16:20
 */

package info.chrm.jtalker;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 *
 * @author  ChrM
 */
public class JTalkerClientFrontPopUp extends javax.swing.JPopupMenu implements JTalkerClientCommandInterface {
    private JTalkerClientFront parent = null;
    private JMenuItem chatItem = null;
    private JMenuItem closeRoomItem = null;
    private JMenuItem addRoomUserItem = null;
    private JMenuItem removeRoomUserItem = null;
    private JTalkerClient.MemberData selectedUser = null;
    
    /** Creates new form JTalkerClientFront */
    public JTalkerClientFrontPopUp(JTalkerClientFront _parent) {
       this.parent = _parent;
       this.initComponents();
       this.parent.theClient.registerForCommandResponse(this);
    }

    /** Initialize GUI elements */
    private void initComponents() {
       if(this.chatItem == null)
	 this.chatItem = new JMenuItem("Open Chat Window");
       
       chatItem.addActionListener(new java.awt.event.ActionListener() { 
	  public void actionPerformed(java.awt.event.ActionEvent e) {
	     showChat();
	  }	    
       });
	     
       this.add(chatItem);

       if(this.closeRoomItem == null)
	 this.closeRoomItem = new JMenuItem("Close Chatroom");      
       closeRoomItem.addActionListener(new java.awt.event.ActionListener() { 
	  public void actionPerformed(java.awt.event.ActionEvent e) {
	     closeChatroom();
	  }	    
       });
	    
       this.closeRoomItem.setVisible(false);
       this.add(closeRoomItem);

       if(this.addRoomUserItem == null)
	 this.addRoomUserItem = new JMenuItem("Add user to this chatroom");      
       addRoomUserItem.addActionListener(new java.awt.event.ActionListener() { 
	  public void actionPerformed(java.awt.event.ActionEvent e) {
	     addChatroomUser();
	  }	    
       });
	    
       this.addRoomUserItem.setVisible(false);
       this.add(addRoomUserItem);

       if(this.removeRoomUserItem == null)
	 this.removeRoomUserItem = new JMenuItem("Remove user from this chatroom");      
       removeRoomUserItem.addActionListener(new java.awt.event.ActionListener() { 
	  public void actionPerformed(java.awt.event.ActionEvent e) {
	     removeChatroomUser();
	  }	    
       });
	    
       this.removeRoomUserItem.setVisible(false);
       this.add(removeRoomUserItem);
    }

    //-------------------------------- ACTIONS ----------------------------------
    /** Show Chat Window */
    private void showChat() {
       this.parent.openChatWindow();
    }
    
    /** Tryes to close a chatroom */
    private void closeChatroom() {	
       this.parent.removeChatroom(this.selectedUser.short_name);       
    }
    
    /** A command answer will be send to us from the server using this method 
     *  @return true, if the _id belongs to our command */
    public boolean setCommandAnswer(String _id, String _from, String _content) {
        if(_id.equals("getallusers01")) {
           String tmpLastResponse = _content;
           
           if(tmpLastResponse == null)
               return false;

           String tmpAllUsers [] = tmpLastResponse.split(";;");
           
           String tmpUser = (String)JOptionPane.showInputDialog(this.parent,
                                                        "User name:",
                                                        "Add User",
                                                        javax.swing.JOptionPane.INFORMATION_MESSAGE,
                                                        null,
                                                        tmpAllUsers,
                                                        null
                                                        );
           if(tmpUser == null)
              return false;
           
           this.parent.addChatroomUser(this.selectedUser.short_name, tmpUser);
           return true;

        } else if(_id.equals("getchatroomusers01")) {
           String tmpLastResponse = _content;

           if(tmpLastResponse == null)
               return false;

           String tmpChatUsers [] = tmpLastResponse.split(";;");
           String tmpUser = (String)JOptionPane.showInputDialog(this.parent,
                                                        "User name:",
                                                        "Remove User",
                                                        javax.swing.JOptionPane.INFORMATION_MESSAGE,
                                                        null,
                                                        tmpChatUsers,
                                                        null
                                                        );
           
           if(tmpUser == null)
              return false;
           
           this.parent.removeChatroomUser(this.selectedUser.short_name, tmpUser);
           return true;
        } else if(_id.equals("rmchatuser01") || 
                _id.equals("addchatuser01") ||
                _id.equals("rmchat01")) {
           // Show message box for answers 
           javax.swing.JOptionPane.showMessageDialog(this.parent, 
                                            _content, 
                                            "Server Answer", 
                                            javax.swing.JOptionPane.INFORMATION_MESSAGE);
        }

        return false;
    }

    /** Tryes to add a chatroom user */
    private void addChatroomUser() {
       // Get name
       this.parent.theClient.sendMessage("std::getAllUsers()", null, "command", null, "getallusers01");       
    }

    /** Tryes to remove a chatroom user */
    private void removeChatroomUser() {
       // Get name
       //String tmpUser = JOptionPane.showInputDialog("User name:");
       this.parent.theClient.sendMessage("std::getChatRoomUsers(\"" + this.selectedUser.short_name + "\")", null, "command", null, "getchatroomusers01");       
    }

    //------------------------------ END ACTIONS --------------------------------

    /** Overwrited show method for enabling only needed actions */
    public void show(Component _invoker, int _x, int _y) {
       this.selectedUser = this.parent.getSelectedUser();
       
       if(this.selectedUser.type.equals(JTalkerParser.USER_TYPE_CHATROOM)) {
	  this.closeRoomItem.setVisible(true);
	  this.addRoomUserItem.setVisible(true);
	  this.removeRoomUserItem.setVisible(true);
       }
       else {
	  this.closeRoomItem.setVisible(false);
	  this.addRoomUserItem.setVisible(false);
	  this.removeRoomUserItem.setVisible(false);
       }
	  
       // Show Popup
       super.show(_invoker, _x, _y);
    }
}
